<?php

namespace app\controllers;

use yii\web\Controller;
use Yii;
use app\models\typeState;
use app\models\type;
use app\models\topic;
use app\models\feedback;
use app\models\topicState;

class TypeController extends Controller{
         public $enableCsrfValidation = false;
    //2.2获取问题类别名称及完成情况（两个功能：1、未答题前，显示整一个typename（类型名列）,2、用户答题后，前端post给我openid，显示完成情况。）
    public function actionGet(){
	      $model = new typeState();
	      if(Yii::$app->request->isPost){
		$postData = Yii::$app->request->post();
	        $openid = $postData['openid'];
	        $sql = "select * from freshmanHelper_typeState where openid = '$openid'";
		$data = typeState::findBysql($sql)->asArray()->all();
		echo json_encode(array('error' =>0,'message' => 'success','data' => $data,));
	}
               	exit(0);
}

	//2.2.1未答题前，显示整一个typename（类型名列）及该类题目数量
     public function actionDisplay(){
	 $model = new type();
         $topic = new topic();
         if(Yii::$app->request->isPost){
            $postData = Yii::$app->request->post();
            $isAll = $postData['isAll'];
            if($isAll == 0){
              for($typeId = 1; $typeId <=10; $typeId++){
              $sql = "select count(topicId) from freshmanHelper_topic where apply = 1 and typeId = '$typeId'";
              $topic_num = topic::findBysql($sql)->asArray()->all();               
              $topic_num = (int)$topic_num[0]['count(topicId)'];
              $count[] = array('typeId' => $typeId,'topicCount' => $topic_num); 
            }  
               $sqld = "select typeId,typename,EnglishName from freshmanHelper_type";
         //    $topic_num = topic::findBysql($sql)->asArray()->all();
         //    $topic_num = (int)$topic_num[0]['count(topicId)'];
               $data = type::findBysql($sqld)->asArray()->all();
               $result[] = array('topicCount' => $count ,'type' => $data);
          //   $result[] = array_merge($count,$data);
               echo json_encode(array('error' => 0,'message' => '新生的topicCount','data' => $result,));      
           }else{
            for($typeId = 1; $typeId <=10; $typeId++){
              $sql = "select count(topicId) from freshmanHelper_topic where typeId = '$typeId'";                   $topic_num = topic::findBysql($sql)->asArray()->all();
              $topic_num = (int)$topic_num[0]['count(topicId)'];
              $count[] = array('typeId' => $typeId,'topicCount' => $topic_num);
            }
	    $sql = "select typeId,typename,EnglishName from freshmanHelper_type";
            $data = type::findBysql($sql)->asArray()->all();
            $result[] = array('topicCount' => $count,'type' => $data);
            echo json_encode(array('error' => 0,'所有的type表' => 'success','data' => $result,));
        }      
      }
    //     exit(0);
  }
 
     //获取特定类别的问题 		 
	public function actionSpcl(){
	   $type = new type();
           $topic = new topic();
           $topicState = new topicState();
	   if(Yii::$app->request->isPost){
                $postData = Yii::$app->request->post();
		$typeId = $postData['typeId'];
                $isAll = $postData['isAll'];
                $openid = $postData['openId'];
                $sqld = "select topicId,openid,state from freshmanHelper_topicState where openid = '$openid' and topicId in (select topicId from freshmanHelper_topic where typeId = $typeId)";
                $state = topicState::findBysql($sqld)->asArray()->all();
                if($isAll == 0){
		 $sql = "select topicId,content,typeId,apply,answer from freshmanHelper_topic where typeId = $typeId and apply = 1";
                 $data = topic::findBysql($sql)->asArray()->all();
                 $new[] = array('topic_state' => $state, 'data' => $data);
		 echo json_encode(array('error' => 0, 'message' => '新生特定类别的疑惑', 'data' => $new,));
          }else{
                 $sql = "select topicId,typeId,content,apply,answer from freshmanHelper_topic where typeId = $typeId";
                 $data = topic::findBysql($sql)->asArray()->all();
                 $new[] = array('topic_state' => $state, 'data' => $data);
                 echo json_encode(array('error' => 0,'message' => '所有的特定类别的疑惑','data' => $new,));
		} 
          }
                 exit(0);			
     }
 
        //反馈新问题
         public function actionFeedback(){
               $model = new feedback();
               if(Yii::$app->request->isPost){
                  $postData = Yii::$app->request->post();
                  $result = $model->add($postData);
                  if($result == 0){
                     $data = array('content' => '提交数据成功');
                     echo json_encode(array('error' => 0,'message' => 'success','data' => $data,));
          }else{
                     $data = array('content' => '提交数据失败，请@留言，联系公众号~','Content' => $result);
                     echo json_encode(array('error' =>1,'message' => 'fail','data' => $data,));             
                }
           }
            //         exit(0);
	}

        //初始化typeState表
                 public function actionInit(){
              $model = new typeState();
              if(Yii::$app->request->isPost){
                   $postData = Yii::$app->request->post();
                   $result = $model->inite($postData);
                   if($result == 0){
                      $data = array('content' => '初始化成功');
                      echo json_encode(array('error' => 0,'message' => 'success','data' => $data,));
              } else{
                      $data = array('content' => '初始化失败','Content' => $result);
                      echo json_encode(array('error' =>1,'message' => 'fail','data' => $data,));             
                   }
             }
              //        exit(0);
    }

          //更新typeState表---2.6答题功能
         public function actionUpdatetype(){
               $model = new typeState();
               if(Yii::$app->request->isPost){
                    $postData = Yii::$app->request->post();
                    $typeId = $postData['typeId'];
                    $openid = $postData['openid'];
                    $completedTopicCount = $postData['completedTopicCount'];
                    $completedScore = $postData['completedScore'];
                    $data = $model -> updateAll(['openid' => $openid, 'completedTopicCount' => $completedTopicCount,'completedScore' => $completedScore],['typeId' => $typeId]);
                    echo json_encode(array('error' => 0,'message' => 'success', 'data' => $data,)); 
              }
                    exit(0);
         }

}
